package com.example.administrator.personalmail.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.personalmail.MyApplication;
import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.api.Contants;
import com.example.administrator.personalmail.model.Page;
import com.example.administrator.personalmail.model.Wares;
import com.example.administrator.personalmail.msg.LoginRespMsg;
import com.example.administrator.personalmail.msg.User;
import com.example.administrator.personalmail.retrofitAPI.GetRequest_Interface;
import com.example.administrator.personalmail.widget.ClearEditText;
import com.example.administrator.personalmail.widget.DESUtil;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends AppCompatActivity {
    private ClearEditText phone;
    private ClearEditText password;
    private Button login;
    private TextView toReg;
    private ImageView ivBack;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViewSet();
    }

    private  void initViewSet(){
        password = findViewById(R.id.etxt_pwd);
        phone = findViewById(R.id.etxt_phone);
        login = findViewById(R.id.btn_login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkLoginMes();
            }
        });
        toReg = findViewById(R.id.txt_toReg);
        toReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegFirstActivity.class);
                startActivity(intent);
            }
        });
        ivBack = findViewById(R.id.iv_back_login);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginActivity.this.finish();
            }
        });
    }

    public void checkLoginMes(){
        String ph = phone.getText().toString();
        if (TextUtils.isEmpty(ph)){
            Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
            return;
        }
        String pw = password.getText().toString();
        if (TextUtils.isEmpty(pw)){
            Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
            return;
        }

        if (ph.equals("1") && pw.equals("1")){
            Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
            setResult(Contants.REQUEST_SUCCESS);
            LoginActivity.this.finish();
        }else {
            Map<String,Object> params = new HashMap<>(2);
            params.put("phone",ph);
            params.put("password", DESUtil.encode(Contants.DES_KEY,pw));

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(Contants.API.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            GetRequest_Interface service = retrofit.create(GetRequest_Interface.class);
            retrofit2.Call<LoginRespMsg<User>> call = service.login(params);

            call.enqueue(new Callback<LoginRespMsg<User>>() {
                @Override
                public void onResponse(Call<LoginRespMsg<User>> call, Response<LoginRespMsg<User>> response) {

                    if (response.body().getStatus() == 1){
                        MyApplication application = MyApplication.getInstance();
                        application.putUser(response.body().getData(), response.body().getToken());
                        setResult(Contants.REQUEST_SUCCESS);
                        LoginActivity.this.finish();
                    }else {
                        Toast.makeText(LoginActivity.this,response.body().getMessage()+"",Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<LoginRespMsg<User>> call, Throwable t) {

                }
            });
        }


    }
}
